package com.xxx.yuedong.community.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.xxx.yuedong.common.R;
import com.xxx.yuedong.common.exception.YuedongException;
import com.xxx.yuedong.community.db.dao.ActivityDao;
import com.xxx.yuedong.community.pojo.Activity;
import com.xxx.yuedong.community.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityDao activityDao;

    @Override
    public int insert(Activity activity) {
        log.debug("开始处理【发布活动】的业务，参数：{}", activity.toString());
        return activityDao.insert(activity);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始处理【删除活动】的业务，参数：{}", id);
        Activity activity = activityDao.selectById(id);
        if (activity == null) {
            String message = "删除活动失败，尝试删除的活动数据不存在！";
            log.warn(message);
            throw new YuedongException(404, message);
        }
        return activityDao.deleteById(id);
    }

    @Override
    public int addActivityById(Long id) {
        log.debug("开始处理【加入活动】的业务，参数：{}", id);
        HashMap activity = activityDao.searchActivityById(id);
        if (activity == null) {
            String message = "加入活动失败，尝试加入活动数据不存在！";
            log.warn(message);
            throw new YuedongException(404, message);
        }
        // 是否在活动已结束
        LocalDateTime endTime = (LocalDateTime) activity.get("end_time");
        LocalDateTime currentTime = LocalDateTime.now();
        if (currentTime.isAfter(endTime)){
            throw new YuedongException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "活动已结束！！");
        }
        String member = (String) activity.get("members");
        ArrayList memberArray = JSONUtil.parse(member).toBean(ArrayList.class);

        for (Object m : memberArray) {
            if (m.toString().equals(StpUtil.getLoginIdAsString())) {
                throw new YuedongException(HttpStatus.SC_NO_CONTENT, "已加入该活动");
            }
        }

        memberArray.add(StpUtil.getLoginIdAsLong());

        HashMap param = new HashMap();
        param.put("id", id);
        param.put("members", memberArray.toString());
        return activityDao.addActivityById(param);
    }

    @Override
    public int checkActivity(Long id) {
        log.debug("开始处理【签到活动】的业务，参数：{}", id);

        HashMap activity = activityDao.searchActivityById(id);
        if (activity == null) {
            String message = "签到活动失败，尝试签到活动数据不存在！";
            log.warn(message);
            throw new YuedongException(404, message);
        }

        // 是否在活动范围时间内，未在该活动开放时间，不能签到
        LocalDateTime startTime = (LocalDateTime) activity.get("start_time");
        LocalDateTime endTime = (LocalDateTime) activity.get("end_time");
        LocalDateTime currentTime = LocalDateTime.now();
        if (currentTime.isBefore(startTime) || currentTime.isAfter(endTime)){
            throw new YuedongException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未在该活动开放时间，不能签到");
        }

        String members = (String) activity.get("members");
        long currentUserId = StpUtil.getLoginIdAsLong();
        if (members != null && members.contains(String.valueOf(currentUserId))) {
            // 用户已加入活动
            String present = (String) activity.get("present");
            HashMap<String, Object> param = new HashMap<>();
            param.put("id", id);
            ArrayList presentArray = new ArrayList<>();
            // 如果实际参与人员名单有人，判断该用户是否已签到
            if (present != null) {
                presentArray = JSONUtil.parse(present).toBean(ArrayList.class);
                for (Object o : presentArray) {
                    if (o.toString().equals(StpUtil.getLoginIdAsString())) {
                        throw new YuedongException(HttpStatus.SC_NO_CONTENT, "已签到该活动");
                    }
                }
            }

            presentArray.add(StpUtil.getLoginIdAsLong());
            param.put("present", presentArray.toString());
            return activityDao.checkActivity(param);
        }else {
            throw new YuedongException(HttpStatus.SC_NO_CONTENT, "你还未加入该活动");
        }

    }

    @Override
    public ArrayList<HashMap> searchAllActivity() {
        log.debug("开始处理【查看所有活动】的业务");
        ArrayList<HashMap> activityList = activityDao.searchAllActivity();
        return activityList;
    }

}
